package com.jswdwsx.esign.config

import com.jswdwsx.esign.protocol.EsignCallAdapterFactory
import com.jswdwsx.esign.protocol.EsignOtherApi
import okhttp3.OkHttpClient
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


@Configuration
class HttpConfig(
    private val esignProperties: EsignProperties
) {
    @Bean
    fun esignTokenApi(@Autowired @Qualifier("commonHttpClient") okHttpClient: OkHttpClient): EsignOtherApi? {
        val retrofit = Retrofit.Builder().baseUrl(esignProperties.host).client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(EsignCallAdapterFactory.create())
            .build()
        return retrofit.create(EsignOtherApi::class.java)
    }

    @Bean
    fun commonHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .readTimeout(100, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build()
    }
}
